#!/bin/bash

# Milis Linux 2.0 easy service management script milisarge-2019
# todo!!! localization

usage="usage: service start|stop|restart|status|install|remove|disable <service_name>"

_err(){
	echo "$1" 
	exit 1
}

service_dir="/usr/milis/ayarlar/servisler/"

[ ! -d $service_dir ] && _err "$service_dir not found!"

[ -z "$1" ] &&  _err "$usage"

[ -z "$2" ] &&  _err "service_name not found"
 
job=$1
service=$2

# service install	
if [ $job == "install" ]; then
	[ ! -f ${service_dir}/mbd/init.d/$service ] && _err "service not found in $service_dir"
	cd $service_dir
	make kur-$service
# service remove
elif [ $job == "remove" ]; then
	[ ! -f /etc/init.d/${service} ] && _err "service not found in /etc/init.d"
	cd $service_dir
	make sil-$service
# service disable / if we don't want to auto-starting of a service
# after this we can start it manually
elif [ $job == "disable" ]; then
	[ ! -f /etc/init.d/${service} ] && _err "service not found in /etc/init.d"
	cd $service_dir
	make sil-$service
	cp -vf ${service_dir}/mbd/init.d/${service} /etc/init.d/
# service start/stop/status
elif [ $job == "start" ] || [ $job == "stop" ] || [ $job == "status" ]; then
	[ ! -f /etc/init.d/${service} ] && _err "service not found in /etc/init.d"
	/etc/init.d/${service} $job
else
	echo "non-valid $job"
	_err "$usage"
fi
